<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="dynamicValidateForm" slot="detail" :rules="rules" :model="form">
        <a-row :gutter="24">
          <a-col :span="24">
            <a-form-model-item
              label="标题"
              prop="title"
              :labelCol="{ span: 3 }"
              :wrapperCol="{ span: 16 }"
              style="margin-bottom: 20px"
              :rules="[
                {
                  required: true,
                  message: '请输入标题',
                  trigger: 'change',
                },
              ]"
            >
              <a-input
                :disabled="isShow"
                :maxLength="20"
                v-model="form.title"
                placeholder="请输入标题"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="大屏播报"
              prop="isBroad"
              :labelCol="{ span: 3 }"
              :wrapperCol="{ span: 16 }"
              style="margin-bottom: 20px"
              :rules="[
                {
                  required: true,
                  message: '请选择是否在大屏播报',
                  trigger: 'change',
                },
              ]"
            >
              <a-radio-group v-model="form.isBroad" :disabled="isShow">
                <a-radio :value="1">是</a-radio>
                <a-radio :value="0">否</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="枇杷文化历史"
              prop="description"
              :labelCol="{ span: 3 }"
              :wrapperCol="{ span: 16 }"
              style="margin-bottom: 20px"
              :rules="[
                {
                  required: true,
                  message: '请输入枇杷文化历史',
                  trigger: 'change',
                },
              ]"
            >
              <a-textarea
                :disabled="isShow"
                :maxLength="120"
                v-model="form.description"
                placeholder="请输入塘栖枇杷文化历史，限制120字以内"
                style="width: 100%"
                :auto-size="{ minRows: 5, maxRows: 6 }"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="枇杷荣誉资质"
              prop="certificateImgUrls"
              :labelCol="{ span: 3 }"
              :wrapperCol="{ span: 16 }"
              style="margin-bottom: 20px"
              :rules="[
                {
                  required: true,
                  message: '请上传枇杷荣誉资质图片',
                  trigger: 'change',
                },
              ]"
            >
              <j-image-upload-custom
                :disabled="isShow"
                listType="picture-card"
                :uploadSize="5"
                :maxLength="5"
                text="上传照片"
                v-model="form.certificateImgUrls"
                :isMultiple="true"
                tip="最多上传5张，只支持.jpg/.png格式，文件大小小于5M"
              ></j-image-upload-custom>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>
<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JMultiSelectTag from '@/components/dict/JMultiSelectTag'
import JImageUploadCustom from '@/components/jeecg/JImageUploadCustom'
export default {
  name: 'culturalHistoryForm',
  components: {
    JFormContainer,
    JImageUploadCustom,
    JDictSelectTag,
    JMultiSelectTag,
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false,
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false,
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    //详情
    isShow: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      id: '',
      isAdd: true,
      result: {},
      form: {
        isBroad: 1,
        title: '',
        description: '',
        certificateImgUrls: undefined,
      },
      model: {},
      labelCol: {
        xs: { span: 3 },
        sm: { span: 2 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 10 },
      },
      confirmLoading: false,
      rules: {},
      url: {
        add: '/loquat/tqppCulturalHistory/add',
        edit: '/loquat/tqppCulturalHistory/edit',
        queryById: '/loquat/tqppCulturalHistory/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    },
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  mounted() {},
  methods: {
    add() {
      this.isAdd = true
      this.edit({})
    },
    edit(record) {
      this.$nextTick(() => {
          this.$refs.dynamicValidateForm.resetFields()
      })
      if (!record.id) {
        this.form.isBroad = 0
        this.form.description = undefined
        this.form.certificateImgUrls = undefined
        this.form.title = undefined
      } else {
        this.id = record.id
        let params = { id: record.id }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.form = res.result
          }
        })
        this.isAdd = false
      }
      this.visible = true
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.id }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result, false)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.dynamicValidateForm.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          this.$emit('loading', that.confirmLoading)
          let httpurl = ''
          let method = ''
          let formData = {}
          if (this.isAdd) {
            console.log(this.form,'this.form');
            console.log(formData,'formData');
            httpurl += this.url.add
            method = 'post'
            formData = this.form
            delete formData.id
            delete formData.sysOrgCode
            delete formData.createTime
            delete formData.createBy
            delete formData.updateBy
            delete formData.updateTime
          } else {
            httpurl += this.url.edit
            method = 'put'
            formData = this.form
            formData.id = this.id
          }
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              this.$emit('loading', that.confirmLoading)
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'title', 'isBroad', 'description', 'certificateImgUrls'))
    },
  },
}
</script>
<style lang="less" scoped>
/deep/.ant-form-item-control{
    line-height: 40px !important;
}
.ant-calendar-picker {
  width: 250.44px;
}
/deep/.varietyForm {
  .ant-table-tbody > tr::hover {
    background: transparent !important;
  }
  .ant-table-tbody > tr > td {
    border-bottom: none;
  }
  .ant-table-tbody .ant-table-row td {
    padding-top: 5px !important;
    padding-bottom: 5px !important;
  }
  .ant-table-thead {
    display: none;
  }
}
.ant-checkbox-wrapper {
  margin: 10px 0;
}
.ant-radio-group {
  padding: 0 10px;
}
.ant-form-item-control {
  line-height: 0px !important;
}
.ant-col-sm-5 {
  width: 100px;
}
.projectstyle {
  .ant-col-sm-16 {
    width: 88%;
    .ant-form-item-control {
      .ant-form-item-children {
        > div {
          width: 95% !important;
        }
      }
    }
  }
}
</style>